<script setup>
import { preloadImage } from '@/utils/tool.js';
import { getPdfResource } from '@/api/lingNan.js'
import VLoading from "@/components/VLoading.vue";

let loading = ref(false)
const pdfResource = reactive([])

/** 初始化当前pdf数据 */
const currentData = reactive({
  title: null,
  firstAuthor: null,
  secondAuthor: null,
  introduce: null,
  img: null,
  url: null,
  bgImg: null
})

/** 图片点击事件 */
const openPDF = ((pdfUrl) => {
  window.open(pdfUrl, '_blank');
})
/** 点击上一页事件 */
const previousPDF = ((val) => {
  loading.value = true
  currentData.bgImg = null
  Object.assign(currentData, pdfResource[currentData.id - 1])
  preloadImage(currentData.img)
  preloadImage(currentData.bgImg)
  loading.value = false
})

/** 点击下一页事件 */
const nextPDF = (() => {
  loading.value = true
  currentData.bgImg = null
  Object.assign(currentData, pdfResource[currentData.id + 1])
  preloadImage(currentData.img)
  preloadImage(currentData.bgImg)
  loading.value = false
})


onMounted(async () => {
  loading.value = true
  await getPdfResource()
    .then(res => {
      if (res.status === 200) {
        res.data.data.forEach(item => {
          pdfResource.push(item.attributes.pdfResource)
          pdfResource.forEach((element, index) => {
            element['id'] = index
            preloadImage(element.img)
            preloadImage(element.bgImg)
          });
          Object.assign(currentData, pdfResource[0])
        })
      }
      else alert('数据请求错误，请联系管理员')
    })
    .catch(err => { console.log(err) })
    .finally(() => { loading.value = false })
})

const isShowLeftArrow = computed(() => currentData.id !== 0);
const isShowRightArrow = computed(() => currentData.id + 1 !== pdfResource.length);
</script>

<template>


  <v-container fluid :style="{ backgroundImage: 'url(' + currentData.bgImg + ')' }">
    <v-row justify="space-around">
      <template v-if="loading">
        <VLoading :isLoading=loading />
      </template>
      <template v-else>
        <v-col cols="5" class="leftContent">
          <img @click="openPDF(currentData.url)" :src="currentData.img">
        </v-col>
        <v-col cols="6" class="rightContent">
          <div class="title">{{ currentData.title }}</div>
          <div class="firstAuthor">{{ currentData.firstAuthor }}</div>
          <div class="secondAuthor">{{ currentData.secondAuthor }}</div>
          <div class="introduce">
            <text style="font-weight: bold; font-size: 1.3em;">简
              介：</text>{{ currentData.introduce }}
          </div>
          <div class="arrow">
            <img v-if="isShowLeftArrow" @click="previousPDF" class="previous" src="/src/assets/images/nextPage.png"
              alt="">
            <img v-if="isShowRightArrow" @click="nextPDF" :class="[{ 'isMove': currentData.id == 0 }, 'next']"
              src="/src/assets/images/nextPage.png" alt="">
          </div>
        </v-col>
      </template>

    </v-row>

    <img class="leftTong" src="https://kanekikeh.online/A/icon/smileTong.png">
    <img class="rightTong" src="https://kanekikeh.online/A/icon/happyTong.png">
  </v-container>


</template>

<style scoped lang="scss">
* {
  margin: 0;
  padding: 0;
}

.v-container--fluid {
  width: 100vw;
  height: 100vh;
  position: relative;
  // background-image: url("https://kanekikeh.online/pdf_files/img/%E5%9B%A2%E5%88%8A.png");
  background-position: center;
  background-size: cover;
}

.v-row {
  width: 80vw;
  height: 80vh;
  position: absolute;
  position: relative;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -45%);
  border: 1px solid #ccc;
  border-radius: 5px;
  box-shadow: rgba(50, 50, 93, 0.25) 0px 2px 5px -1px, rgba(0, 0, 0, 0.3) 0px 1px 3px -1px;
  background-color: white;
}

.loadingCircle {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.leftContent {
  position: relative;

  & img {
    width: 70%;
    height: 90%;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    border-radius: 5px;
    // box-shadow: rgba(0, 0, 0, 0.16) 0px 3px 6px, rgba(0, 0, 0, 0.23) 0px 3px 6px;
    box-shadow: rgba(50, 50, 93, 0.25) 0px 6px 12px -2px, rgba(0, 0, 0, 0.3) 0px 3px 7px -3px;
    cursor: pointer;
    // transition: transform 0.3s ease;

    // &:hover {
    //   transform: translate(-50%, -50%) scale(1.1);
    // }
  }
}

.rightContent {
  display: flex;
  flex-direction: column;
  justify-content: strat;
  align-items: start;
  height: 90%;
  margin-left: -13%;
  margin-top: 2%;
  color: #65656a;

  .title {
    font-size: 2em;
    color: #3c3c43;
    font-weight: bold;
    margin-bottom: 10px;
  }

  .firstAuthor {
    font-size: 0.9em;
    font-weight: bold;
    margin-bottom: 3px;
  }

  .secondAuthor {
    font-size: 0.9em;
    font-weight: bold;
  }

  .introduce {
    font-size: 1.1em;
    margin-top: 5vh;
    text-align: justify;
    letter-spacing: 1px;
  }

  .arrow {
    display: flex;
    flex-direction: row;
    width: 100%;
    margin-top: auto;

    .next {
      width: 60px;
      height: 50px;
      cursor: pointer;
      margin-left: auto;
    }

    .previous {
      width: 60px;
      height: 50px;
      cursor: pointer;
      transform: rotate(180deg);
    }
  }
}

.isMove {
  animation: move 2s infinite ease-in-out;
  -webkit-animation: move 2s infinite ease-in-out;
  -moz-animation: move 2s infinite ease-in-out;
}

.isMoveLeft {
  // transform: rotate(180deg);
  animation: move 2s infinite ease-in-out;
  -webkit-animation: move 2s infinite ease-in-out;
  -moz-animation: move 2s infinite ease-in-out;
}

@keyframes move {
  0% {
    transform: translateX(0);
  }

  50% {
    transform: translateX(10px);
  }

  100% {
    transform: translateX(0);
  }
}

.leftTong {
  width: 150px;
  height: 150px;
  transform: rotate(30deg);
  position: fixed;
  bottom: -3%;
  left: -3%;
}

.rightTong {
  width: 140px;
  height: 150px;
  transform: rotate(330deg);
  position: fixed;
  bottom: -3%;
  right: -2.1%;
}
</style>